package com.lmabbe.tenant.service.impls;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lmabbe.common.global.context.TenantBuildContext;
import com.lmabbe.tenant.mapper.TenantInstallContextMapper;
import com.lmabbe.tenant.objects.converter.TenantInstallContextConverter;
import com.lmabbe.tenant.objects.dto.TenantInstallContextDTO;
import com.lmabbe.tenant.objects.entity.TenantInstallContext;
import com.lmabbe.tenant.service.TenantInstallContextService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 租户安装的上下文
 *
 * @author lmabbe
 */
@Service
@RequiredArgsConstructor
public class TenantInstallContextServiceImpl extends ServiceImpl<TenantInstallContextMapper, TenantInstallContext>
        implements TenantInstallContextService {

    private final TenantInstallContextConverter tenantInstallContextConverter;
    private final KafkaTemplate<String, Object> kafkaTemplate;

    @SneakyThrows
    @Override
    public TenantInstallContext build(TenantInstallContextDTO contextDTO) {

        TenantInstallContext installContext = baseMapper.selectById(contextDTO.getTenantId());

        Boolean isInstall = null == installContext;

        ObjectMapper jsonMapper = new ObjectMapper();
        String version = SecureUtil.md5(jsonMapper.writeValueAsString(contextDTO));

        TenantInstallContext installContextDo = tenantInstallContextConverter.dto2Entity(contextDTO);
        installContextDo.setVersion(version);
        installContextDo.setInstall(isInstall);

        buildSysParam(installContextDo);

        if (isInstall) {
            installContextDo.setInstallDate(new Date());
            this.baseMapper.insert(installContextDo);
        } else {
            if (version.equals(installContext.getVersion())) {
                return null;
            }
            this.baseMapper.updateById(installContextDo);
        }
        return installContext;
    }

    @Override
    public void buildAndPublish(TenantInstallContextDTO contextDTO) {
        TenantInstallContext build = this.build(contextDTO);
        if (build != null) {
            this.publish(build);
        }
    }

    /**
     * 发布，发送mq，让业务服务器处理
     *
     * @param installContext 租户安装上下文
     * @date 2022/2/21 5:57 PM
     * @author liming
     */
    public void publish(TenantInstallContext installContext) {
        TenantBuildContext tenantBuildContext = JSONObject.parseObject(JSONObject.toJSONString(installContext), TenantBuildContext.class);
        kafkaTemplate.send("system", "tenant", tenantBuildContext);
    }

    /**
     * 构建系统参数
     *
     * @param installContextDo
     */
    private void buildSysParam(TenantInstallContext installContextDo) {
        List<TenantInstallContextDTO.ContextSysParamsDTO> sysParams = installContextDo.getSysParams();
        sysParams.add(new TenantInstallContextDTO.ContextSysParamsDTO());
    }


}
